<?php

    class FinanceController extends Controller {
        public $metaDescription = null;
        public $metaKeywords = null;
        public $linkCanoncical = null;
        public $title = null;
        public $layout = 'column_project';        

        /*Trang chủ*/
        public function actionIndex(){
            $url = new Url();         
            $project_id = $_SESSION['project_pm']['project_id'];
            $data_project = Project::getRowById($project_id);
            $data = Finance::getRowByProjectID($project_id);                    
            $this->render("index",array("data"=>$data,"data_project"=>$data_project));        
        }

        public function actionAdd(){
            $url = new Url();                     
            $project_id = $_SESSION['project_pm']['project_id'];
            $data_project = Project::getRowById($project_id);
            $data = Finance::getRowByProjectID($project_id);                    
            $this->render("add",array("data"=>$data,"data_project"=>$data_project));        
        }

        public function actionAjaxAddFinance(){            

            $finance_name = isset($_GET["finance_name"])?mysql_escape_string($_GET["finance_name"]):"";
            $finance_spent = isset($_GET["finance_spent"])?mysql_escape_string($_GET["finance_spent"]):"";
            $finance_description = isset($_GET["finance_description"])?mysql_escape_string($_GET["finance_description"]):"";

            $array_input['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_input['finance_name'] = $finance_name;
            $array_input['finance_spent'] = $finance_spent;
            $array_input['finance_description'] = $finance_description;                    
            $array_input['finance_create_user'] = $_SESSION['project_pm']['project_id'];
            $array_input['finance_create_time'] = time();
            $last_id = Finance::insertObject($array_input);

            $data_project = Project::getRowById($_SESSION['project_pm']['project_id']);
            $array_budget['project_remain'] = $data_project['project_remain'] - $finance_spent;

            $update_project = Project::updateObject($array_budget,"project_id",$_SESSION['project_pm']['project_id']);


            /*$project_name = $_SESSION['project_pm']['project_name'];
            $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name thêm mới danh mục $category_name";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    */

            if($last_id !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

        public function actionAjaxDeleteFinance(){            

            $finance_id = isset($_GET["finance_id"])?mysql_escape_string($_GET["finance_id"]):"";
            $project_id = $_SESSION['project_pm']['project_id'];
            
            $data = Finance::getRowById($finance_id);
            $temp = $data['finance_spent'];
            
            $data = Project::getRowById($project_id);
            $temp = $temp + $data['project_remain'];
            
            $array_input_project['project_remain'] = $temp;    
            Project::updateObject($array_input_project,"project_id",$project_id);
            
            $array_input['finance_id'] = $finance_id;    
            $last_id = Finance::deleteObject($array_input);
            
            /*$project_name = $_SESSION['project_pm']['project_name'];
            $array_activity['project_id'] = $_SESSION['project_pm']['project_id'];
            $array_activity['activity_user_id'] = $_SESSION['user_pm']['user_id'];
            $array_activity['activity_name'] = "Dự án $project_name xóa danh mục";
            $array_activity['activty_type'] = 1;
            $array_activity['activity_update_time'] = time();        
            $log = Activity::insertObject($array_activity);    */

            if($last_id !=0){            
                echo $temp;
            }else{
                echo $temp;
            }
        }

        public function actionAjaxSendEmailFinance(){            

            $finance_name = isset($_GET["finance_name"])?mysql_escape_string($_GET["finance_name"]):"";
            $finance_spent = isset($_GET["finance_spent"])?mysql_escape_string($_GET["finance_spent"]):"";
            $finance_description = isset($_GET["finance_description"])?mysql_escape_string($_GET["finance_description"]):"";            
            $finance_spent = number_format($finance_spent);
            $user_name = $_SESSION['user_pm']['fullname'];                    
            $messege = "Đề xuất tài chính : $finance_name của $user_name. <br> Nội dung là : $finance_description. <br> Số tiền yêu cầu : $finance_spent";            
            $mail = $_SESSION['project_pm']['email'];
            $send_mail = SendMail::sendMail($mail,$messege);
            echo 1;die;
            if($send_mail !=0){            
                echo 1;                                           
            }else{
                echo 0;
            }
        }

    }

?>